/*	
	pptthh AS3 utils / Flex - ActionScript
	Copyright (c) 2009 Peter Toth <pptthh@gmail.com>
	Your reuse is governed by the Creative Commons Attribution 3.0 License
*/
/*	****************************************************************************
 *	How to use it:
 * 	
 *	copy these lines, to your class and call them insted of "trace()"	=)
 */
//	import	com.tools.traceTool.TraceTool;
//	//	normal message			balck	white
//	private	function	Trace(...args):void{TraceTool.send('Trace',String(Object(this).constructor),args);}
//	//	error message			red		bold
// 	private	function	eTrace(...args):void{TraceTool.send('eTrace',String(Object(this).constructor),args);}
//	//	warning message			orange	bold
//	private	function	wTrace(...args):void{TraceTool.send('wTrace',String(Object(this).constructor),args);}
//	//	highlighted message		white	bold
//	private	function	hTrace(...args):void{TraceTool.send('hTrace',String(Object(this).constructor),args);}

//	//	and also 			g-green	b-blue	y-yellow	m-magenta	c-cyan	s-silver	colors can be used	(all bold)
//	//	or one of these: 	gTrace 	bTrace	yTrace		mTrace		cTrace	sTrace
/*	****************************************************************************
 */
package org.utils.send
{
	import flash.events.AsyncErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.StatusEvent;
	import flash.events.TimerEvent;
	import flash.net.LocalConnection;
	import flash.sampler.Sample;
	import flash.sampler.pauseSampling;
	import flash.system.Capabilities;
	import flash.system.System;
	import flash.utils.Timer;	
	/**
	 * @author pptthh@gmail.com
	 */
// DEF #####################################################
//
public class DeveloperTools extends DevConfig
{
	private		static	const	CLIENT_ID:Number=(new Date()).getTime();
	private		static	const	SEPARATOR:String='	|';
	private		static	var		localConnection:LocalConnection
	private		static	var		isFirst:Boolean=true;
	private		static	var		remoteMethod:String=null;
	private		static	var		remoteClassID:String=null;
	private		static	var		passTroughArguments:Array=null;
// DEF #####################################################
//public function DeveloperTools():void{}
//
public	static	function	send(CalledMethodName:String,...args):void
{
	flash.sampler.pauseSampling();	//	only for debugger versions after
	var	className:String=args.shift();
	passTroughArguments = String(args).split('\n');
	for(var i:uint = passTroughArguments.length; i--;) {sendSL();}
	flash.sampler.startSampling();	//	only for debugger versions after
}
// Private #################################################
//	send a Signle Line, line brakes will be removed!
private	static	function	sendSL():void
{
	localConnection = new LocalConnection();
	localConnection.allowDomain(		ALLOW_DOMAIN, ALLOW_DOMAIN_HOST);
	localConnection.allowInsecureDomain(ALLOW_DOMAIN, ALLOW_DOMAIN_HOST);
	localConnection.addEventListener(	StatusEvent.STATUS,					onStatus);
	localConnection.addEventListener(	AsyncErrorEvent.ASYNC_ERROR,		onAsyncError);
	localConnection.addEventListener(	SecurityErrorEvent.SECURITY_ERROR,	onSecurityError);
	isFirst?runOnce():null;
	remoteMethod.length>1?
		localConnection.send( CONNECTION_ID,remoteMethod,passTroughArguments.shift(),passTroughArguments):
		localConnection.send( CONNECTION_ID,remoteMethod,passTroughArguments);
	localConnection.removeEventListener(StatusEvent.STATUS,					onStatus);
	localConnection.removeEventListener(AsyncErrorEvent.ASYNC_ERROR,		onAsyncError);
	localConnection.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,	onSecurityError);
	localConnection = null;
}
//	to make a signal for chacking the connection...
private	static	function	runOnce():void
{
	isFirst=false;
	localConnection.send( DevConfig.CONNECTION_ID, "connected");
	send('onTrace',String(({}).constructor),flashPlayerInfo);
	var	timer:Timer=new Timer( DevConfig.SIGN);
	Capabilities.isDebugger?
		timer.addEventListener( TimerEvent.TIMER,sendSign):
		timer.addEventListener( TimerEvent.TIMER,sendSignNonDBG);
		timer.start();
}
private	static	function	get	flashPlayerInfo():String
{
	var	s:String='\n\t';
	return ''+
	s+'os, mem., lang.: 		'+Capabilities.os+'; '+System.totalMemory+'; '+Capabilities.language+
	s+'manuf., type, ver.:		'+Capabilities.manufacturer+'; '+Capabilities.playerType+'; '+Capabilities.version+
	s+'screen:					'+Capabilities.screenColor+'	'+Capabilities.screenResolutionX+'*'+Capabilities.screenResolutionY+'	'+Capabilities.screenDPI+'DPI	Ratio:'+Capabilities.pixelAspectRatio+
	s+'localFileReadDisable:	'+Capabilities.localFileReadDisable+
	s+'hasScreenBroadcast:		'+Capabilities.hasScreenBroadcast+
	s+'avHardwareDisable:		'+Capabilities.avHardwareDisable+
	s+'hasAccessibility:		'+Capabilities.hasAccessibility+
	s+'hasPrinting: 			'+Capabilities.hasPrinting+
	s+'hasAudio:				'+Capabilities.hasAudio+
	s+'hasMP3:					'+Capabilities.hasMP3+
	s+'hasAudioEncoder: 		'+Capabilities.hasAudioEncoder+
	s+'hasStreamingAudio:		'+Capabilities.hasStreamingAudio+
	s+'hasScreenPlayback:		'+Capabilities.hasScreenPlayback+
	s+'hasEmbeddedVideo:		'+Capabilities.hasEmbeddedVideo+
	s+'hasVideoEncoder: 		'+Capabilities.hasVideoEncoder+
	s+'isDebugger:				'+Capabilities.isDebugger+
	s;
}
private	static	function	get memoriInfo():String
{
	var newObj:int=0;
	var newObjSize:int=0;
	var delObj:int=0;
	var delObjSize:int=0;
	var cpuObj:int=0;
	var cpuObjSize:int=0;
	var unknown:int=0;
	
	for each(var s:Sample in flash.sampler.getSamples())	switch (String(s))
	{
		case	'[NewObjectSample]':	newObj++;	newObjSize+= flash.sampler.getSize(s);	continue;
		case	'[DeleteObjectSample]':	delObj++;	delObjSize+= flash.sampler.getSize(s);	continue;
		case	'[Sample]':				cpuObj++;	cpuObjSize+= flash.sampler.getSize(s);	continue;
		case	'[StackFrame]':
		default: 						unknown++; 
	}
	return	''+
		newObj+				SEPARATOR+
		newObjSize+			SEPARATOR+
		delObj+				SEPARATOR+
		delObjSize+			SEPARATOR+
		cpuObj+				SEPARATOR+
		cpuObjSize+			SEPARATOR+
		unknown+			SEPARATOR+
		System.totalMemory;
}
//private static function get timeStamp():Number {return (new Date()).getTime();}
// Events ##################################################
//
private	static	function	onAsyncError(e:AsyncErrorEvent):void {trace('	AsyncError		'+e.target+e.text);}
private	static	function	onSecurityError(e:SecurityErrorEvent):void {trace('	SecurityError	'+e.target+e.text);}
private	static	function	onStatus(e:StatusEvent):void{e.level=='status'||e.level=='error'?null:trace('	onStatus		'+e.target+'	'+e.type+'	'+e.level);}
private	static	function	sendSignNonDBG(e:TimerEvent):void{localConnection.send(DevConfig.CONNECTION_ID,'signNonDBG');}
private	static	function	sendSign(e:TimerEvent):void
{
	flash.sampler.pauseSampling();
	localConnection.send(DevConfig.CONNECTION_ID,'sign',memoriInfo);
	flash.sampler.clearSamples();
	flash.sampler.startSampling();
}

// END #####################################################
}	//class
}	//package
// END #####################################################